<template>
  <el-form ref="registerForm" :model="User" :rules="Rules" label-width="100px" class="registerForm">
    <el-form-item label="用户名" prop="name">
      <el-input v-model="User.name" placeholder="Enter Username..."></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="User.email" placeholder="Enter Email..."></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="User.password" type="password" placeholder="Enter Password..."></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="password2">
      <el-input v-model="User.password2" type="password" placeholder="Enter Password..."></el-input>
    </el-form-item>

    <!-- <el-form-item label="选择身份">
      <el-select v-model="User.role" placeholder="请选择身份">
        <el-option label="管理员" value="admin"></el-option>
        <el-option label="用户" value="user"></el-option>
        <el-option label="游客" value="visitor"></el-option>
      </el-select>
    </el-form-item> -->

    <el-form-item>
      <el-button @click="handleRegister('registerForm')" type="primary">注册</el-button>
      <el-button @click="resetForm()" type="info">重置</el-button>
      <!-- 回到登录 -->
      <div class="tiparea">
        <p><a @click="sendchange('true')">回到登录</a></p>
      </div>

    </el-form-item>
  </el-form>
</template>

<script lang="ts">
import { getCurrentInstance } from 'vue'
export default {
  props: {
    User: {
      type: Object,
      required: true,
    },
    Rules: {
      type: Object,
      required: true,
    },
  },

  setup() {
    // @ts-ignore
    const { ctx } = getCurrentInstance()

    // 注册
    const handleRegister = (formName: string) => {
      ctx.$refs[formName].validate((valid: boolean) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }

    // 重置表单
    const resetForm = () => {
      ctx.$refs['registerForm'].resetFields()
    }

    // 发送事件，激活父组件send2事件
    const sendchange = (isregedit: boolean): void => {
      resetForm()
      ctx.$emit("send2", isregedit);
    };

    return { handleRegister, resetForm, sendchange }
  },
}
</script>

<style scoped>
.registerForm {
  margin-top: 20px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #333;
}
.tiparea p a {
  color: #409eff;
  margin: 10px;
  cursor: pointer;
} 
</style>
